home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / EnterAct Stuff / hAWK project / AWK Source / ALLOCA.C next >
Text File  |  1995-04-08  |  5KB  |  200 lines

  1. /*
  2.     alloca -- (mostly) portable public-domain implementation -- D A Gwyn
  3.  
  4.     last edit:    86/05/30    rms
  5.        include config.h, since on VMS it renames some symbols.
  6.        Use xmalloc instead of malloc.
  7.  
  8.     This implementation of the PWB library alloca() function,
  9.     which is used to allocate space off the run-time stack so
  10.     that it is automatically reclaimed upon procedure exit, 
  11.     was inspired by discussions with J. Q. Johnson of Cornell.
  12.  
  13.     It should work under any C implementation that uses an
  14.     actual procedure stack (as opposed to a linked list of
  15.     frames).  There are some preprocessor constants that can
  16.     be defined when compiling for your specific system, for
  17.     improved efficiency; however, the defaults should be okay.
  18.  
  19.     The general concept of this implementation is to keep
  20.     track of all alloca()-allocated blocks, and reclaim any
  21.     that are found to be deeper in the stack than the current
  22.     invocation.  This heuristic does not reclaim storage as
  23.     soon as it becomes invalid, but it will do so eventually.
  24.  
  25.     As a special case, alloca(0) reclaims storage without
  26.     allocating any.  It is a good idea to use alloca(0) in
  27.     your main control loop, etc. to force garbage collection.
  28. */
  29.  
  30. /*
  31.     Define STACK_DIRECTION if you know the direction of stack
  32.     growth for your system; otherwise it will be automatically
  33.     deduced at run-time.
  34.  
  35.     STACK_DIRECTION > 0 => grows toward higher addresses
  36.     STACK_DIRECTION < 0 => grows toward lower addresses
  37.     STACK_DIRECTION = 0 => direction of growth unknown
  38. */
  39. /* Modified for THINK C 4 on the Mac by Ken Earle (Dynabyte) Aug 1991. */
  40. #define MACVERSION
  41. /* Mac stack grows down */
  42. #define    STACK_DIRECTION    -1
  43.  
  44. typedef void    *pointer;        /* generic pointer type */
  45.  
  46. #include <stdlib.h>
  47. #include <unix.h>
  48.  
  49. #ifndef NULL
  50. #define NULL        ((void *) 0)
  51. #endif
  52.  
  53. #define malloc(x) (void *)NewPtr(x)
  54. #define free(x) DisposPtr(x)
  55.  
  56. pointer alloca (unsigned short size);
  57. pointer    xmalloc(unsigned short n);
  58.  
  59.  
  60. #ifndef STACK_DIRECTION
  61. #define    STACK_DIRECTION    0        /* direction unknown */
  62. #endif
  63.  
  64. #if STACK_DIRECTION != 0
  65.  
  66. #define    STACK_DIR    STACK_DIRECTION    /* known at compile-time */
  67.  
  68. #else    /* STACK_DIRECTION == 0; need run-time code */
  69.  
  70. static short    stack_dir;        /* 1 or -1 once known */
  71. #define    STACK_DIR    stack_dir
  72.  
  73. static void
  74. find_stack_direction (/* void */)
  75. {
  76.   static char    *addr = NULL;    /* address of first
  77.                    `dummy', once known */
  78.   auto char    dummy;        /* to get stack address */
  79.  
  80.   if (addr == NULL)
  81.     {                /* initial entry */
  82.       addr = &dummy;
  83.  
  84.       find_stack_direction ();    /* recurse once */
  85.     }
  86.   else                /* second entry */
  87.     if (&dummy > addr)
  88.       stack_dir = 1;        /* stack grew upward */
  89.     else
  90.       stack_dir = -1;        /* stack grew downward */
  91. }
  92.  
  93. #endif    /* STACK_DIRECTION == 0 */
  94.  
  95. /*
  96.     An "alloca header" is used to:
  97.     (a) chain together all alloca()ed blocks;
  98.     (b) keep track of stack depth.
  99.  
  100.     It is very important that sizeof(header) agree with malloc()
  101.     alignment chunk size.  The following default should work okay.
  102. */
  103.  
  104. #ifndef    ALIGN_SIZE
  105. #define    ALIGN_SIZE    sizeof(short)
  106. #endif
  107.  
  108. typedef union hdr
  109. {
  110.   char    align[ALIGN_SIZE];    /* to force sizeof(header) */
  111.   struct
  112.     {
  113.       union hdr *next;        /* for chaining headers */
  114.       char *deep;        /* for stack depth measure */
  115.     } h;
  116. } header;
  117.  
  118. /*
  119.     alloca( size ) returns a pointer to at least `size' bytes of
  120.     storage which will be automatically reclaimed upon exit from
  121.     the procedure that called alloca().  Originally, this space
  122.     was supposed to be taken from the current stack frame of the
  123.     caller, but that method cannot be made to work for some
  124.     implementations of C, for example under Gould's UTX/32.
  125. */
  126.  
  127. static header *last_alloca_header = NULL; /* -> last alloca header */
  128.  
  129. pointer
  130. alloca (unsigned short size)            /* returns pointer to storage */
  131.      /* unsigned    size;         # bytes to allocate */
  132. {
  133.   auto char    probe;        /* probes stack depth: */
  134.   register char    *depth = &probe;
  135.  
  136. #if STACK_DIRECTION == 0
  137.   if (STACK_DIR == 0)        /* unknown growth direction */
  138.     find_stack_direction ();
  139. #endif
  140.  
  141.                 /* Reclaim garbage, defined as all alloca()ed storage that
  142.                    was allocated from deeper in the stack than currently. */
  143.  
  144.   {
  145.     register header    *hp;    /* traverses linked list */
  146.  
  147.     for (hp = last_alloca_header; hp != NULL;)
  148.       if (/* STACK_DIR > 0 && hp->h.deep > depth
  149.       || STACK_DIR < 0 && */ hp->h.deep < depth)
  150.     {
  151.       register header    *np = hp->h.next;
  152.  
  153.       free ((pointer) hp);    /* collect garbage */
  154.  
  155.       hp = np;        /* -> next header */
  156.     }
  157.       else
  158.     break;            /* rest are not deeper */
  159.  
  160.     last_alloca_header = hp;    /* -> last valid storage */
  161.   }
  162.  
  163.   if (size == 0)
  164.     return NULL;        /* no allocation required */
  165.  
  166.   /* Allocate combined header + user data storage. */
  167.  
  168.   {
  169.     register pointer    new = xmalloc (sizeof (header) + size);
  170.     /* address of header */
  171.  
  172.     ((header *)new)->h.next = last_alloca_header;
  173.     ((header *)new)->h.deep = depth;
  174.  
  175.     last_alloca_header = (header *)new;
  176.  
  177.     /* User storage begins just after header. */
  178.  
  179.     return (pointer)((char *)new + sizeof(header));
  180.   }
  181. }
  182.  
  183. pointer xmalloc(unsigned short n)
  184. {
  185. #ifdef MACVERSION
  186.   extern void JumpOnHAWKError(short inputErrorNumber);
  187. #endif
  188.   pointer cp;
  189.   static char mesg[] = "xmalloc: no memory!\n";
  190.  
  191.   cp = malloc(n);
  192.   if (! cp) {
  193.       fwrite (mesg, 1, sizeof(mesg) - 1, stderr);
  194. #ifdef MACVERSION
  195.     JumpOnHAWKError(1);
  196. #endif
  197.   }
  198.   return cp;
  199. }
  200.